package com.yunxi.service.dubboService.impl.sat;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.yunxi.model.sat.SatTestDriveRoute;
import com.yunxi.sat.pojo.SatTestDriveRouteListVO;
import com.yunxi.sat.pojo.SatTestDriveRouteVO;
import com.yunxi.sat.service.SatTestDriveRouteDubboService;
import com.yunxi.service.localService.CodeCreateService;
import com.yunxi.service.localService.sat.SatTestDriveRouteService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.List;

/**
 * <p>
 * 试乘试驾路线 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-07-09
 */
public class SatTestDriveRouteDubboServiceImpl implements SatTestDriveRouteDubboService {

    @Autowired
    @Qualifier(value = "satTestDriveRouteServiceImpl")
    SatTestDriveRouteService satTestDriveRouteService;

    @Autowired
    @Qualifier(value = "codeCreateServiceImpl")
    CodeCreateService codeCreateService;

    @Override
    public SatTestDriveRouteVO getById(String id) throws Exception {
        SatTestDriveRoute satTestDriveRoute = satTestDriveRouteService.getById(id);
        if(satTestDriveRoute != null){
            SatTestDriveRouteVO satTestDriveRouteVO = new SatTestDriveRouteVO();
            BeanUtils.copyProperties(satTestDriveRoute,satTestDriveRouteVO);
            return satTestDriveRouteVO;
        }
        else {
            throw new Exception("操作失败,无法根据主键id:"+id+"找到对应试驾路线!");
        }
    }

    @Override
    public void saveOrUpdate(SatTestDriveRouteVO satTestDriveRouteVO) throws Exception {
        SatTestDriveRoute satTestDriveRoute = new SatTestDriveRoute();
        BeanUtils.copyProperties(satTestDriveRouteVO,satTestDriveRoute);
        if(satTestDriveRoute.getTestDriveRouteId() == null){
            String tdr = codeCreateService.createCode("TDR", 4);
            satTestDriveRoute.setTestDriveRouteNo(tdr);
        }
        //路线名称不可重复
        checkRouteName(satTestDriveRouteVO, satTestDriveRoute);
        satTestDriveRouteService.saveOrUpdate(satTestDriveRoute);
    }

    /**
     * 校验路线名称是否以存在
     * @param satTestDriveRouteVO
     * @param satTestDriveRoute
     * @throws Exception
     */
    private void checkRouteName(SatTestDriveRouteVO satTestDriveRouteVO, SatTestDriveRoute satTestDriveRoute) throws Exception {
        QueryWrapper<SatTestDriveRoute> wrapper = new QueryWrapper<>();
        wrapper.eq("ROUTE_NAME",satTestDriveRouteVO.getRouteName());
        wrapper.eq("IS_DEL",0);
        if(satTestDriveRouteVO.getTestDriveRouteId() != null ){
            wrapper.ne("TEST_DRIVE_ROUTE_ID",satTestDriveRoute.getTestDriveRouteId());
        }
        String partnerCode = satTestDriveRouteVO.getPartnerCode();
        if(partnerCode != null && StringUtil.isNotEmpty(partnerCode)){
            wrapper.eq("PARTNER_CODE",partnerCode);
        }
        List<SatTestDriveRoute> list = satTestDriveRouteService.list(wrapper);
        if(list != null && list.size() > 0){
            throw new Exception("操作失败,路线名称:"+satTestDriveRouteVO.getRouteName()+"已存在!");
        }
    }

    @Override
    public void removeById(String id) {
        satTestDriveRouteService.removeById(id);
    }

    @Override
    public PageInfo<SatTestDriveRouteVO> findPageByVO(SatTestDriveRouteVO satTestDriveRouteVO, Integer pageNum, Integer pageSize) throws Exception {
        PageInfo<SatTestDriveRouteVO> pageInfo = satTestDriveRouteService.pageByVo(satTestDriveRouteVO, pageNum, pageSize);
        return pageInfo;
    }

    @Override
    public void removeByListVo(List<SatTestDriveRouteVO> satTestDriveRouteVOList) throws Exception {
        satTestDriveRouteService.removeByListVo(satTestDriveRouteVOList);
    }

    @Override
    public List listByVo(SatTestDriveRouteVO satTestDriveRouteVO) {
        return satTestDriveRouteService.listByVo(satTestDriveRouteVO);
    }

    @Override
    public List<SatTestDriveRouteListVO> listByCode(String partnerCode) {
        return satTestDriveRouteService.listByCode(partnerCode);
    }

    @Override
    public void removeRoute(List<String> list, String updateBy) throws Exception {
        if(list != null && list.size() > 0){
            for (String id : list){
                SatTestDriveRoute byId = satTestDriveRouteService.getById(id);
                if(byId != null){
                    byId.setIsDel(1);
                    byId.setUpdateBy(updateBy);
                    satTestDriveRouteService.updateById(byId);
                }
                else {
                    throw new Exception("操作失败,无法根据id:"+id+"找到对应试驾路线!");
                }
            }
        }
        else{
            throw new Exception("操作失败,list为空!");
        }
    }
}
